package ai4games_tests.maths;

import game2dai.maths.Geometry2D;
import processing.core.PApplet;

public class TangentsToCircle extends PApplet {

	final int IRAD = 5;

	float x,y,cx,cy,r;
	float[] tpoint = null;
	boolean intersect;

	public void setup(){
		size(400,300);
		cursor(CROSS);
		cx = width/3;
		cy = height/3;
		r = 40;
		x = 100;
		y = 100;
		ellipseMode(RADIUS);
	}

	public void draw(){
		background(128);
		x = mouseX;
		y = mouseY;
		tpoint = makeFloatArray(Geometry2D.tangents_to_circle(x, y, cx, cy, r));
		strokeWeight(1.1f);
		if(tpoint.length > 0)
			stroke(255,0,0);
		else
			stroke(0,255,0);
		// Draw circle
		fill(255,255,192,140);
		ellipse(cx,cy,r,r);

		fill(200,200,255);	
		for(int p = 0; p < tpoint.length; p += 2){
			stroke(255,0,0);
			strokeWeight(1.2f);
			line(x,y,tpoint[p], tpoint[p+1]);
			noStroke();
			ellipse(tpoint[p], tpoint[p+1], IRAD, IRAD);
		}
		if(tpoint.length == 0){
			fill(255,255,0);
			text("No tangents possible from inside the circle", 10, height - 4);
		}
	}
	
	public float[] makeFloatArray(double[] da){
		float[] fa = new float[da.length];
		for(int i = 0; i < fa.length; i++)
			fa[i] = (float)da[i];
		return fa;
	}
	


}
